package com.security.client;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.security.entity.User;
import com.security.entity.UserInfo;
import com.security.entity.UserOauth;
import com.security.feign.ITestUserClient;
import com.security.service.IUserOauthService;
import com.security.service.IUserService;
import com.security.vo.UserOauthVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author cici
 */
@RestController
@Slf4j
public class TUserClient implements ITestUserClient {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserOauthService userOauthService;

    @GetMapping(USER_INFO)
    @Override
    public UserInfo uInfo(String account) {
        UserInfo info = new UserInfo();
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("account",account);
        User user = userService.getOne(queryWrapper);
        List<String> roles = new ArrayList<String>();
        roles.add("administrator");
        info.setUser(user);
        info.setRoles(roles);
        log.info("info:" + info.toString());
        return info;
    }

    @GetMapping(USER_INFO_BY_PHONE)
    @Override
    public UserInfo queryUserInfoByUsername(String phone) {
        UserInfo info = new UserInfo();
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("account",phone);
        User user = userService.getOne(queryWrapper);
        List<String> roles = new ArrayList<String>();
        roles.add("administrator");
        info.setUser(user);
        info.setRoles(roles);
        log.info("info:" + info.toString());
        return info;
    }

    @Override
    @GetMapping(INSERT_MINI_INFO)
    public UserOauthVO insertMiniUserByJson(String wxJson, String openId) {
        if (StringUtils.isEmpty(wxJson)){
            return null;
        }
        Map<String, String> userOauthVOMap = (Map<String, String>) JSONObject.parse(wxJson);
        UserOauth userOauth = new UserOauth();
        userOauth.setNickname(userOauthVOMap.get("nickName"));
        userOauth.setAvatar(userOauthVOMap.get("avatarUrl"));
        userOauth.setGender(String.valueOf(userOauthVOMap.get("gender")));
        userOauth.setOpenid(userOauthVOMap.get("openId"));
        userOauth.setSource("MINI_PROGRAM");
        userOauth.setLocation(userOauthVOMap.get("country") + userOauthVOMap.get("province") + userOauthVOMap.get("city"));
        UserOauth oldUser = userOauthService.getOne(Wrappers.<UserOauth>lambdaQuery().eq(UserOauth::getOpenid, openId));
        if (ObjectUtils.isNull(oldUser)) {
            userOauthService.save(userOauth);
        } else {
            userOauth.setId(oldUser.getId());
            userOauthService.updateById(userOauth);
        }
        //查询出最新用户

        UserOauthVO resUser = new UserOauthVO();
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("account","cici");
        User user = userService.getOne(queryWrapper);
        List<String> roles = new ArrayList<String>();
        roles.add("administrator");
        resUser.setRoles(roles);
        resUser.setUser(user);
        return resUser;
    }
}
